﻿//-----------------------------------------------------
// 脚本控制
// 这是一个 Honorbuddy 的 bot 程序
// 文件名 Controller.cs
// 请将此文件放入 Honorbuddy/bots/Controller/ 目录内
// 作者 ccddnet 讨论组QQ群 64715569
// SVN: http://hb-controller.googlecode.com/svn/trunk/
//-----------------------------------------------------
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Media;
using Styx.Common;
using Styx.CommonBot;
using Styx.Helpers;
using Styx.Loaders;
using Styx.TreeSharp;
using Action = Styx.TreeSharp.Action;

namespace CcddNet
{
    public sealed class XmlLoader : BotBase
    {
        private static bool _initialize;
        private BotBase _controller = null;

        private XmlLoader()
        {
            if (_initialize)
                return;
            _initialize = true;
            string asmName = Assembly.GetExecutingAssembly().GetName().Name;
            int len = asmName.LastIndexOf("_", StringComparison.Ordinal);
            string folderName = asmName.Substring(0, len);
            string botsPath = GlobalSettings.Instance.BotsPath;
            if (!Path.IsPathRooted(botsPath))
                botsPath = Path.Combine(Utilities.AssemblyDirectory, botsPath);
            botsPath = Path.Combine(botsPath, folderName);
            string asmVer = new AssemblyName(Assembly.GetCallingAssembly().FullName).Version.Revision.ToString();
            string fileName = string.Format("Controller_{0}.dll", asmVer);
            DirectoryInfo dir = new DirectoryInfo(botsPath);
            FileInfo[] ffs = dir.GetFiles("*.dll");
            for (int i = 0; i < ffs.Length; i++)
            {
                try
                {
                    if (ffs[i].Name.ToLower() != fileName.ToLower())
                        ffs[i].Delete();
                }
                catch { }
            }
            botsPath = Path.Combine(botsPath, fileName);
            if (!File.Exists(botsPath))
            {
                using (WebClient client = new WebClient())
                {
                    string url = string.Concat("http://hb-controller.googlecode.com/svn/trunk/", fileName);
                    try
                    {
                        client.DownloadFile(url, botsPath);
                    }
                    catch { }
                }
                if (!File.Exists(botsPath))
                {
                    Log("下载失败请稍候再试");
                    _initialize = false;
                    return;
                }
            }
            try
            {
                Assembly asm = Assembly.LoadFile(botsPath);
                DllLoader<BotBase> dll = new DllLoader<BotBase>(asm, null);
                if (dll.Count > 0)
                    _controller = dll[0];
            }
            catch { }
            if (_controller == null)
            {
                _initialize = false;
                Failed();
            }
        }
        ~XmlLoader()
        {
            Stop();
        }
        public override string Name 
        { 
            get 
            { 
                return "脚本控制"; 
            }
        }
        public override PulseFlags PulseFlags 
        {
            get 
            {
                if (_initialize)
                    return _controller.PulseFlags;
                else
                    return PulseFlags.All;
            }
        }
        public override Composite Root 
        { 
            get
            {
                if (_initialize)
                    return _controller.Root;
                else
                    return new Action();
            }
        }
        public override Form ConfigurationForm
        {
            get
            {
                if (_initialize)
                    return _controller.ConfigurationForm;
                else
                    return null;
            }
        }
        public override void Start()
        {
            if (_initialize)
                _controller.Start();
            else
                Failed();
        }
        public override void Stop()
        {
            if (_initialize)
                _controller.Stop();
        }
        private void Failed()
        {
            Log("加载程序集失败");
        }
        private void Log(string str)
        {
            Logging.Write(Colors.LightBlue, string.Format("[脚本控制]{0}", str));
        }
    }
}
